<script>
import { defineComponent } from "vue";

import { ordersList , ordersEdit} from "@/api/orders";
import { mapState, mapMutations } from "vuex";
import { EditPen } from "@element-plus/icons-vue";
export default defineComponent({
  data() {
    return {
      // 接口传递的参数
      page: 1,
      pagesize: 27,
      // list接收传递的订单列表
      list: [],
      // total: 0,
      dialogFormVisible: false,
      currentPage: 1,
      form: {
        order_price: 0,
        order_pay: '',
        is_send: '',

      },
      // 分页数据
      currentNum: 1,
      pageSize: 10,
    };
  },
  computed: {
    ...mapState(["order_price"]),
    // 分页
    data() {
      return this.list.slice((this.currentNum - 1) * this.pageSize, (this.currentNum - 1) * this.pageSize + this.pageSize)
    }
  },

  methods: {
    ...mapMutations(["setOrderPrice"]),
    requestList() {
      ordersList({
        pagenum: this.page,
        pagesize: this.pagesize,
      }).then((res) => {
        console.log(res);
        if (res.data.meta.status === 200) {
          this.list = res.data.data.goods;
          // console.log(res.data.data.goods);
          this.total = res.data.data.total;
        }
      });
    },

    edit(data) {
      this.dialogFormVisible = true
      this.setOrderPrice(data.order_price)
      this.form.order_price = this.order_price
      // console.log(this.order_price)
    },
    sure() {
      this.dialogFormVisible = false
      return ElMessage.error('修改数据失败')
    }
  },
  mounted() {
    // 请求订单列表
    ordersList({
        pagenum: this.page,
        pagesize: this.pagesize,
      }).then((res) => {
        if (res.data.meta.status === 200) {
          this.list = res.data.data.goods;
          this.pagesize = res.data.data.goods.length
          this.total = res.data.data.total;
        }
      });
    // 修改订单
    ordersEdit({
      order_id:63,

    }).then(res => {
      // console.log(res);
    })
  },
  watch: {
    page: "requestList",
  
  },
  components:{
    EditPen
  },

});
</script>

<template>
  <div class="pro_box">
    <el-table :data="data" style="width: 100%">
      <el-table-column type="index" label="序号" width="100" />
      <el-table-column prop="order_number" label="订单编号" width="200" />
      <el-table-column prop="order_price" label="订单价格" width="100" />
      <el-table-column prop="order_pay" label="付款方式" width="100">
        <template #default="data">
          <el-tag type="danger" v-if="data.row.order_pay === '0'">未付款</el-tag>
          <el-tag type="success" v-if="data.row.order_pay === '1'">支付宝</el-tag>
          <el-tag type="promary" v-if="data.row.order_pay === '2'">微信</el-tag>
          <el-tag type="warning" v-if="data.row.order_pay === '3'">银行卡</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="create_time" label="订单更新时间" width="120" />
      <el-table-column prop="is_send" label="是否发货" width="100" />
      <el-table-column label="操作">
        <!-- Form -->
        <template #default="xxx">
          <div>
            <el-button >
            <span @click="edit(xxx.row)" style="color: blue;"><el-icon>
                <EditPen />
              </el-icon>编辑</span>
              </el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination 
    background 
    layout="prev, pager, next" 
    :total="list.length" 
    v-model:page-size="pageSize"
      v-model:current-page="currentNum" />


  </div>

  <el-dialog v-model="dialogFormVisible" title="编辑订单">
    <el-form :model="form">
      <el-form-item label="订单价格" class="form_box">
        <el-input autocomplete="off" v-model="form.order_price" />
      </el-form-item>
      <el-form-item label="支付方式" :label-width="formLabelWidth">
        <el-select placeholder="支付宝" v-model="form.order_pay">
          <el-option label="支付宝" value="支付宝" />
          <el-option label="微信" value="微信" />
          <el-option label="银行卡" value="银行卡" />
          <el-option label="银行卡" value="银行卡" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否发货" :label-width="formLabelWidth">
        <el-select v-model="form.is_send" placeholder="是">
          <el-option label="是" value="是" />
          <el-option label="否" value="否" />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="sure">
          确定
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>
  
<style lang="scss" scoped>
.form_box {
  width: 290px;
}

.pro_box {
  text-align: center;
}
</style>